// import fetch from 'isomorphic-fetch'

import 'whatwg-fetch';
import * as types from './ActionTypes'
/**
 * get请求
 * @param  {String} options.url   api地址
 * @param  {String} options.query query参数
 * @return {Promise}               Promise
 */

 const _get = ({ url, query },dispatch) => {
  dispatch({type:types.START_LOADING})
  let _url
  if (query) {
    _url = `http://rap.taobao.org/mockjsdata/12185${url}?${query}`
  } else {
    _url = `http://rap.taobao.org/mockjsdata/12185${url}`
  }

  return $.ajax({
    url:_url,
    type:'GET',
    success:function(data){
      console.log(data);
    }
  })
}

/**
 * 获取正在热映电影列表
 * @param  {Function} options.dispatch store对象解构出来的函数，无需手动提供
 * @param  {Number} page             页数
 * @param  {Number} count             每页数量
 * @return {Promise}                  Promise
 */

 export const index_data = (page,count) => {
  const url = '/index';
  // const query = `count=${count}&page=${page}&_t=`+new Date().getTime()
  return (dispatch) =>{
    $.ajax({
      url:'http://rap.taobao.org/mockjsdata/12185/index',
      type:'GET',
      beforeSend:function(){
        dispatch({type:types.START_LOADING});
        console.log('beforeSend');
      },
      success:function(json){
        if(json.status === 'ok'){
          dispatch({type:types.FETCH_INDEX_DATA, index_data:json})
        }
      },
      error:function(error){
        alert('error____:' + error);
      },
      complete:function(){
        dispatch({type:types.FINISH_LOADING})
        console.log('complete');
      }
    })
  }
}
